<?php
include_once(_APP_PATH. "business/media/media_handler.php");
class MediaHandlerPhoto extends MediaHandler
{
	private $_fileObj;	
	
	protected function InitFile()
	{
		$this->_fileObj = $this->Libs()->image->GetImage($this->_filePath);
		if (!$this->_fileObj || !$this->_fileObj->IsImage())	
		{			
			$this->SetError($this->_fileObj->GetError());
			return;
		}
		
		$details = $this->_fileObj->GetImageInfo();
		
		$maxImageSize = MozgCfg::GetProp('global/media//maxImageSize');
		
		if (($maxImageSize[0] < $details[0]) ||
			($maxImageSize[1] < $details[1]))
		{
			$this->SetError('Максимальные размеры фото: ' .$maxImageSize[0]. 'x' .$maxImageSize[1]);
			return;			
		}
		
		$this->_fileSize = $details['filesize'];
		$this->_fileLength = $details['0']. 'x' .$details['1'];	
	}
	
	public function CreateThumbNail()
	{		
		if (false === $this->_fileThumbNail) $this->_fileThumbNail = $this->GetFilePath();
		return $this->_fileThumbNail;
	}
	
	public function Convert($code)
	{
		$destPath = $this->GetDestPath();
		if (false === $destPath) 
		{
			$this->SetConvertStatus(MatchMediaStatus::HasError);
			$this->SetError('Произошла ошибка при обработке файла');
			return false;
		}
		
		$newFileName = pathinfo($this->_filePath, PATHINFO_FILENAME);
		$newFullFileName = $this->_fileObj->Resize($destPath, $newFileName, MozgCfg::GetProp('global/media//imageSizeTo')); 
		
		if ($this->_fileObj->IsError()) 
		{    
			$this->SetConvertStatus(MatchMediaStatus::HasError);
			$this->SetError($this->_fileObj->GetError());          
		    return false;
		} 

		$newFilePath = $destPath.$newFullFileName;
		$this->Init($newFilePath);

		$this->SetConvertStatus(MatchMediaStatus::Ready);
		
		return $newFilePath; 		
	}
}